package com.tian.service.impl;

import com.alibaba.fastjson.JSON;
import com.tian.common.CommonResult;
import com.tian.dto.ChargeStationGunReqDto;
import com.tian.dto.ChargeStationGunRespDto;
import com.tian.entity.ChargeStationGun;
import com.tian.enums.ResultCode;
import com.tian.es.service.ChargeStationEsService;
import com.tian.mapper.ChargeStationGunMapper;
import com.tian.service.ChargeStationGunService;
import com.tian.util.StationRedisPreKey;
import org.redisson.api.RBucket;
import org.redisson.api.RList;
import org.redisson.api.RedissonClient;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * {@code @description:} 充电枪信息
 *
 * @author tianwc 公众号：Java后端技术全栈
 * 在线刷题 1200+java面试题和1000+篇技术文章：<a href="https://woaijava.cc/">博客地址</a>
 * {@code @date:} 2024-03-13 11:10
 * {@code @version:} 1.0
 */
@Service
public class ChargeStationGunServiceImpl implements ChargeStationGunService {
    @Resource
    private ChargeStationGunMapper chargeStationGunMapper;
    @Resource
    private RedissonClient redissonClient;
    @Resource
    private ChargeStationEsService chargeStationEsService;
    @Override
    public CommonResult<Boolean> add(ChargeStationGunReqDto chargeStationGunReqDto) {
        ChargeStationGun chargeStationGun = new ChargeStationGun();
        BeanUtils.copyProperties(chargeStationGunReqDto, chargeStationGun);
        chargeStationGunMapper.insert(chargeStationGun);
        RList<String> list = redissonClient.getList(StationRedisPreKey.STATION_GUN_LIST_KEY + chargeStationGun.getStationId());
        list.add(chargeStationGun.getGunNo());
        setRedis(chargeStationGun);
        return CommonResult.success(true);
    }

    @Override
    public CommonResult<Boolean> update(ChargeStationGunReqDto chargeStationGunReqDto) {
        ChargeStationGun chargeStationGun = new ChargeStationGun();
        BeanUtils.copyProperties(chargeStationGunReqDto, chargeStationGun);
        chargeStationGunMapper.updateByPrimaryKey(chargeStationGun);
        ChargeStationGun chargeStationGunNew = chargeStationGunMapper.selectByPrimaryKey(chargeStationGun.getId());
        setRedis(chargeStationGunNew);
        return CommonResult.success(true);
    }

    private void setRedis(ChargeStationGun chargeStationGun) {
        RBucket<String> chargeStationGunBucket = redissonClient.getBucket(StationRedisPreKey.STATION_GUN_PRE_KEY + chargeStationGun.getId());
        chargeStationGunBucket.set(JSON.toJSONString(chargeStationGun));
        RBucket<String> chargeStationGunBucketByNo = redissonClient.getBucket(StationRedisPreKey.STATION_GUN_NO_PRE_KEY + chargeStationGun.getGunNo());
        chargeStationGunBucketByNo.set(JSON.toJSONString(chargeStationGun));
        RBucket<String> chargeStationGunBucketByStationId = redissonClient.getBucket(StationRedisPreKey.STATION_GUN_NO_PRE_KEY + chargeStationGun.getStationId());
        chargeStationGunBucketByStationId.set(JSON.toJSONString(chargeStationGun));
    }

    @Override
    public CommonResult<ChargeStationGunRespDto> queryById(Integer id) {
        RBucket<String> chargeStationGunBucket = redissonClient.getBucket(StationRedisPreKey.STATION_GUN_PRE_KEY + id);
        ChargeStationGun chargeStationGun = JSON.parseObject(chargeStationGunBucket.get(), ChargeStationGun.class);
        if (chargeStationGun == null) {
            chargeStationGun = chargeStationGunMapper.selectByPrimaryKey(id);
            if (chargeStationGun == null) {
                return CommonResult.failed(ResultCode.PARAMETER_ERROR, "充电枪id=" + id + "不存在");
            }
            chargeStationGunBucket.set(JSON.toJSONString(chargeStationGun));

            RBucket<String> chargeStationGunBucketByNo = redissonClient.getBucket(StationRedisPreKey.STATION_GUN_NO_PRE_KEY + chargeStationGun.getGunNo());
            chargeStationGunBucketByNo.set(JSON.toJSONString(chargeStationGun));

            ChargeStationGunRespDto chargeStationGunRespDto = new ChargeStationGunRespDto();
            BeanUtils.copyProperties(chargeStationGun, chargeStationGunRespDto);
            return CommonResult.success(chargeStationGunRespDto);
        }
        ChargeStationGunRespDto chargeStationGunRespDto = new ChargeStationGunRespDto();
        RBucket<String> chargeStationGunBucketByNo = redissonClient.getBucket(StationRedisPreKey.STATION_GUN_NO_PRE_KEY + chargeStationGun.getGunNo());
        chargeStationGunBucketByNo.set(JSON.toJSONString(chargeStationGun));
        BeanUtils.copyProperties(chargeStationGun, chargeStationGunRespDto);
        return CommonResult.success(chargeStationGunRespDto);
    }

    @Override
    public CommonResult<Boolean> list() {
        return null;
    }
}
